package com.zking.web.ypf;
import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.zking.dao.domain.Information;
import com.zking.dao.domain.SysUser;
import com.zking.dao.domain.User;
import com.zking.service.ypf.INserteCom;
import com.zking.utils.RedisUtil;
@Controller
public class CompleteInformation {
	@Autowired
	private INserteCom inse;
	@Autowired
	private RedisUtil redisUtil;

	//	@RequestMapping(value = "/ins")
	//	public String ins(Complete complete,MultipartFile file) {
	//		try {
	//			/**
	//	         * 上传图片
	//	         */
	//	        //图片上传成功后，将图片的地址写到数据库
	//	        String filePath = "E:\\upload";//保存图片的路径
	//	        //获取原始图片的拓展名
	//	        String originalFilename = file.getOriginalFilename();
	//	        //新的文件名字，使用uuid随机生成数+原始图片名字，这样不会重复
	//	        String newFileName = UUID.randomUUID()+originalFilename;
	//	        //封装上传文件位置的全路径，就是硬盘路径+文件名
	//	        File targetFile = new File(filePath,newFileName); 
	//	        //把本地文件上传到已经封装好的文件位置的全路径就是上面的targetFile
	//	        file.transferTo(targetFile);
	//	        complete.setImage(newFileName);//文件名保存到实体类对应属性上
	//	       // System.out.println(complete);
	//		} catch (Exception e) {
	//			e.printStackTrace();
	//		}
	//		System.out.println(complete);
	//		int i=inse.incom(complete);
	//		if(i!=0) {
	//			return "tag";
	//		}else {
	//			return "index01";
	//		}
	//	}
	@RequestMapping(value = "/selectUser")
	public String selectUser(Information information,HttpServletRequest request,HttpSession session) {
		//request.getSession().getAttribute("InfroUser");
		Object object= request.getSession().getAttribute("InfroUser");
		User user=(User) object;
		System.out.println("user:"+user);
		User newUser= inse.selectUser(user);
		//request.getSession().setAttribute("newUser", newUser);
		session.setAttribute("newUser", newUser);
		System.out.println("newUser:"+newUser);
		information.setInformation_id(newUser.getId());
		inse.ins(information);
		return "index01";
	}

	@RequestMapping(value = "/setReObj1")
	public String setReObj1(Information information,MultipartFile file) {
		try {
			/**
			 * 上传图片
			 */
			//图片上传成功后，将图片的地址写到数据库
			String filePath = "E:\\upload";//保存图片的路径
			//获取原始图片的拓展名
			String originalFilename = file.getOriginalFilename();
			//新的文件名字，使用uuid随机生成数+原始图片名字，这样不会重复
			String newFileName = UUID.randomUUID()+originalFilename;
			//封装上传文件位置的全路径，就是硬盘路径+文件名
			File targetFile = new File(filePath,newFileName); 
			//把本地文件上传到已经封装好的文件位置的全路径就是上面的targetFile
			file.transferTo(targetFile);
			information.setCom_logo(newFileName);//文件名保存到实体类对应属性上
			redisUtil.set("information", JSONObject.toJSONString(information));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "tag";
	}

	@RequestMapping(value = "/setReObj2")
	public String setReObj2(Information information) {
		//将原来存储的Information的json数据取出来，再往里面存储一些数据
		Information redisinformation=JSONObject.parseObject(redisUtil.get("information").toString(),Information.class);
		//redisinformation.setCom_label(information.getCom_label());
		redisinformation.setCom_label(information.getCom_label());
		//将原来的Information进行覆盖
		System.out.println("information.getCom_label()"+information.getCom_label());
		redisUtil.set("information", JSONObject.toJSONString(redisinformation));
		return "founder";

	}

	@RequestMapping(value = "/setReObj3")
	public String setReObj3(Information information,MultipartFile file) {
		try {
			/**
			 * 上传图片
			 */
			//图片上传成功后，将图片的地址写到数据库
			String filePath = "E:\\upload";//保存图片的路径
			//获取原始图片的拓展名
			String originalFilename = file.getOriginalFilename();
			//新的文件名字，使用uuid随机生成数+原始图片名字，这样不会重复
			String newFileName = UUID.randomUUID()+originalFilename;
			//封装上传文件位置的全路径，就是硬盘路径+文件名
			File targetFile = new File(filePath,newFileName); 
			//把本地文件上传到已经封装好的文件位置的全路径就是上面的targetFile
			file.transferTo(targetFile);
			information.setBoss_picture(newFileName);
			//将原来存储的Information的json数据取出来，再往里面存储一些数据
	    	Information redisinformation=JSONObject.parseObject(redisUtil.get("information").toString(),Information.class);
	    	redisinformation.setCom_boss(information.getCom_boss());
	    	redisinformation.setBoss_picture(information.getBoss_picture());
	    	redisinformation.setCom_position(information.getCom_position());
	    	redisinformation.setCom_weibo(information.getCom_weibo());
	    	redisinformation.setCom_founder(information.getCom_founder());
	    	//将原来的Information进行覆盖
	    	redisUtil.set("information", JSONObject.toJSONString(redisinformation));
		} catch (Exception e) {
			e.printStackTrace();
		}
    	//return redisUtil.get("information").toString();
		return "index02";
	}
	@RequestMapping("/setReObj4")
	public String setReObj4(Information information,MultipartFile file) {
		try {
			//图片上传成功后，将图片的地址写到数据库
			String filePath = "E:\\upload";//保存图片的路径
			//获取原始图片的拓展名
			String originalFilename = file.getOriginalFilename();
			//新的文件名字，使用uuid随机生成数+原始图片名字，这样不会重复
			String newFileName = UUID.randomUUID()+originalFilename;
			//封装上传文件位置的全路径，就是硬盘路径+文件名
			File targetFile = new File(filePath,newFileName); 
			//把本地文件上传到已经封装好的文件位置的全路径就是上面的targetFile
			file.transferTo(targetFile);
			information.setCom_poster(newFileName);
			//将原来存储的Information的json数据取出来，再往里面存储一些数据
			Information redisinformation=JSONObject.parseObject(redisUtil.get("information").toString(),Information.class);
			//把新值再set进Redis
			redisinformation.setCom_poster(information.getCom_poster());
			redisinformation.setProduct_name(information.getProduct_name());
			redisinformation.setProduct_brief(information.getProduct_brief());
			redisinformation.setProduct_address(information.getProduct_address());
			//将原来的Information进行覆盖
			redisUtil.set("information", JSONObject.toJSONString(redisinformation));
		} catch (Exception e) {
			e.printStackTrace();
		}
		//return redisUtil.get("information").toString();
		return "index03";
	}
	@RequestMapping("/setReObj5")
	public String setReObj5(Information information) {
		//将原来存储的Information的json数据取出来，再往里面存储一些数据
		Information redisinformation=JSONObject.parseObject(redisUtil.get("information").toString(),Information.class);
		//把新值再set进Redis
		redisinformation.setCom_introduction(information.getCom_introduction());
		redisinformation.setCom_license(information.getCom_license());
		//将原来的Information进行覆盖
		redisUtil.set("information", JSONObject.toJSONString(redisinformation));
		//return redisUtil.get("information").toString();
		return "redirect:/clearUserObj";
	}

	/**
	 * 	直接删除缓存
	 */
	@RequestMapping("/clearUserObj")
	public String clearUserObj(Information information,HttpServletRequest request,HttpSession session) {
		//String redisUserStr = redisUtil.get("information").toString();
		//request.getSession().getAttribute("newUser");
		//Object object=request.getSession().getAttribute("newUser");
		Object object = session.getAttribute("newUser");
		User user=(User) object;
		System.out.println("objUser:"+user);
		System.out.println("你他妈的："+user.getId());
		Information Newinformation=JSONObject.parseObject(redisUtil.get("information").toString(),Information.class);
		Newinformation.setInformation_id(user.getId());
		Newinformation.setCom_audit("未审核");
		System.out.println("Newinformation"+Newinformation.getInformation_id());
		int i= inse.incom(Newinformation);
		//删除redis缓存
		redisUtil.del("information");
		if(i!=0) {
			return "success";
		}else {
			return "index03";
		}
	}
}
